#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>

#include "calculate.h"
#include "mem_manage.h"
#include "doggie.h"
#include "my_msg_queue.h"

#define FIFO_NAME  	"myfifo"
#define BUF_SIZE    	1024



int main(int argc,char * argv[],char ** environ)
{
    
	int readsize=0;
	int queueid;
	
	union_num unum;        
	int count=0;
	int amount=0;
	unsigned char desIp[4];

	unsigned char    buf[1500];
	unsigned char * ucptr;
	memset(buf,0,1500);

//----------消息队列-------------------------------

	queueid=getMsgQueue();
	

//-------------------------------------------------

//-------------------共享内存-------------------------
	int semid;
	void *smptr;
	semid=getSem(SEM_NAME,SEM_ID,GET);
	mem_start=getMem(MEM_NAME,MEM_ID,TOTAL_LEN,GET);
	smptr=mem_start; 
	
//--------------------------------------------

	

	while(1)
	{
		int i;
		int msglen;//length of the packet
		while((readsize=my_read(&smptr,buf,semid))==READ_FAILED)
		{
			sleep(1);
		}
		amount+=readsize;
		count++;

		//打印收到的数据包：
		printf("%dst packet is :\n",count);
		for(i = 0; i < readsize; i++)
		{
			printf("%.2x ", buf[i]);
			if(i % 16 == 15)
				printf("\n");
		}
	
		printf("\n");

		//------拦截索放行的试验--------------------------------------
		
		My_Msg mymsg;
		int msg_len;
		//当识别一个数据包为结束包的时候，把该数据包的识别信息，写到消息队列中，作为判决意见
		if((checkEndPack(buf,readsize))==1)
		{
			LinkId *lid;
			Judge_Msg *mymsg=NULL;
			struct PacId pacid;
			int judgement=1;
	
			printf("proc 收到一个重传包\n");
			msg_len=sizeof(My_Msg)-4;
	
			lid=getLinkId(buf);
			memcpy(&pacid,lid,sizeof(struct PacId));
			free(lid);

			mymsg=getJudgeMsg(pacid,judgement);
			sendJudge(mymsg,queueid);
			
		/*
			memcpy(&(mymsg.pacid),lid,sizeof(LinkId));
			mymsg.msgtype=5;
			
			printf("proc 睡眠5s.....\n");
			sleep(5);
			printf("proc 发送判决意见\n");
			msgsnd(queueid,&mymsg,msg_len,0);
		*/
			
		}
		

	}        
    exit (0);
}
